package com.jzlin.funhub.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzlin.funhub.admin.model.dto.ArticlePageDTO;
import com.jzlin.funhub.admin.model.req.*;
import com.jzlin.funhub.admin.model.resp.UserArticleCommentPageResp;
import com.jzlin.funhub.admin.model.resp.UserDetailResp;
import com.jzlin.funhub.admin.model.resp.UserFollowedPageResp;
import com.jzlin.funhub.admin.model.resp.UserLoginResp;
import com.jzlin.funhub.admin.service.article.ArticleStarService;
import com.jzlin.funhub.admin.service.system.SysUserService;
import com.jzlin.funhub.admin.service.system.UserFollowService;
import com.jzlin.funhub.framework.core.http.RestRet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户管理控制器.
 *
 * @author linjz
 * @since 2025/7/11
 */
@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ArticleStarService articleStarService;

    @Autowired
    private UserFollowService userFollowService;

    @ApiOperation("登录")
    @PostMapping("/doLogin")
    public RestRet<UserLoginResp> doLogin(@RequestBody UserLoginReq req) {
        return RestRet.createSuccessRet(sysUserService.doLogin(req));
    }

    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public RestRet<Void> logout() {
        StpUtil.logout();
        return RestRet.createSuccessRet();
    }

    @ApiOperation("校验是否登录")
    @GetMapping("/login/check")
    public RestRet<UserLoginResp> checkLogin() {
        UserLoginResp resp = sysUserService.checkLogin();
        if (resp != null) {
            return RestRet.createSuccessRet(resp);
        }
        return RestRet.createFailureRet("未登录");
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public RestRet<UserLoginResp> register(@RequestBody UserRegisterReq req) {
        sysUserService.register(req);
        return RestRet.createSuccessRet();
    }

    @ApiOperation("查询用户明细信息")
    @GetMapping("/detail")
    public RestRet<UserDetailResp> getDetail(@RequestParam Long userId) {
        return RestRet.createSuccessRet(sysUserService.getDetail(userId));
    }

    @ApiOperation("分页查询文章评论")
    @PostMapping("/article/comment/page")
    public RestRet<IPage<UserArticleCommentPageResp>> checkLogin(@RequestBody UserArticleCommentPageReq req) {
        return RestRet.createSuccessRet(sysUserService.queryUserArticleCommentPage(req));
    }

    @ApiOperation("分页查询用户收藏文章")
    @PostMapping("/article/star/page")
    public RestRet<IPage<ArticlePageDTO>> queryUserArticleStarPage(@RequestBody @Valid UserArticleStarPageReq req) {
        return RestRet.createSuccessRet(articleStarService.queryUserArticleStarPage(req));
    }

    @ApiOperation("更新用户信息")
    @PostMapping("/update")
    public RestRet<Void> update(@RequestBody @Valid UserInfoUpdateReq req) {
        sysUserService.updateUserInfo(req);
        return RestRet.createSuccessRet();
    }

    @ApiOperation("关注/取消关注用户")
    @PostMapping("/follow")
    public RestRet<Void> followUser(@RequestBody @Valid UserFollowReq req) {
        userFollowService.followUser(req);
        return RestRet.createSuccessRet();
    }

    @ApiOperation("分页查询用户粉丝")
    @PostMapping("/followed/page")
    public RestRet<Page<UserFollowedPageResp>> queryPageUserFollowed(@RequestBody @Valid UserFollowedPageReq req) {
        return RestRet.createSuccessRet(userFollowService.queryPageUserFollowed(req));
    }

    @ApiOperation("分页查询用户关注列表")
    @PostMapping("/follow/page")
    public RestRet<Page<UserFollowedPageResp>> queryPageUserFollow(@RequestBody @Valid UserFollowPageReq req) {
        return RestRet.createSuccessRet(userFollowService.queryPageUserFollow(req));
    }

    @ApiOperation("修改密码")
    @PostMapping("/pwd/update")
    public RestRet<Void> updatePwd(@RequestBody @Valid UserPwdUpdateReq req) {
        sysUserService.updatePwd(req);
        return RestRet.createSuccessRet();
    }
}
